package com.yuqiaotech.dbpro.proxy;

import java.sql.SQLException;
import java.sql.Wrapper;
import java.util.HashMap;
import java.util.Map;

public class PropertiesProxyImpl implements PropertiesProxy,Wrapper{

	Map<String,Object> properties;
	
	PropertiesProxyImpl() {
		this(null);
	}
	
	PropertiesProxyImpl(Map<String,Object> properties) {
		if(null != properties) {
			this.properties = properties;
		} else {
			initProperties();
		}
	}
	
	
	private void initProperties() {
		properties = new HashMap<String,Object>();
	}
	
	@Override
	public Map<String, Object> getProperties() {
		return properties;
	}

	@Override
	public void setProperties(Map<String,Object> properties) {
		this.properties = properties;
		
	}

	@Override
	public Object getProperty(String key) {
		if(null == properties) {
			return null;
		}
		
		return properties.get(key);
	}

	@Override
	public void setProperty(String key, String value) {
		if(null == properties) {
			return;
		}
		properties.put(key,value);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		if(null == iface) {
			return null;
		}
		
		if(iface.isAssignableFrom(PropertiesProxyImpl.class)) {
			return (T)this;
		}
		
		return null;
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {

		if(iface ==  PropertiesProxyImpl.class
				|| iface == PropertiesProxy.class) {
			return true;
		}
		
		return false;
	}

}
